#ifndef __ARCH_TIMER_H__
#define __ARCH_TIMER_H__
#include <stdint.h>
#include <stdbool.h>
#include <arch.h>

#define CNTV_CTL_ENABLE_BIT (1 << 0)
#define CNTV_CTL_IMASK_BIT (1 << 1)

static inline void arch_timer_enable(bool en)
{
    uint32_t cntv_ctl;

    cntv_ctl = read_sysreg(cntv_ctl_el0);

    if (en) {
        cntv_ctl |= CNTV_CTL_ENABLE_BIT;
    } else {
        cntv_ctl &= ~CNTV_CTL_ENABLE_BIT;
    }

    write_sysreg(cntv_ctl_el0, cntv_ctl);
}

static inline void arch_timer_interrupt_enable(bool en)
{
    uint32_t cntv_ctl;

    cntv_ctl = read_sysreg(cntv_ctl_el0);

    if (!en) {
        cntv_ctl |= CNTV_CTL_IMASK_BIT;
    } else {
        cntv_ctl &= ~CNTV_CTL_IMASK_BIT;
    }

    write_sysreg(cntv_ctl_el0, cntv_ctl);
}

static inline uint32_t arch_timer_frequecy(void)
{
    uint32_t rate = read_sysreg(cntfrq_el0);

    return (rate != 0) ? rate : 1000000;
}

static inline uint64_t arch_timer_count(void)
{
    return read_sysreg(cntvct_el0);
}

static inline void arch_timer_compare(uint32_t interval)
{
    write_sysreg(cntv_tval_el0, interval);
}

#endif /* __ARCH_TIMER_H__ */

